<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Site_entity{
	var $current_dbprefix = "";
	var $id = 0;
	var $name = '';
	var $ref = '';
	var $domain = '';
	var $active = 0;
	var $created_on = 0;
	var $updated_on = 0;
	
	function __construct() {
		$ci= & get_instance();
		$this->current_dbprefix = $ci->db->dbprefix;
	}
	function __destruct() {
		$ci= & get_instance();
	}
	
	function setId($input){
		$this->id=$input;
	}
	function setName($input){
		$this->name = $input;
	}
	function setRef($input){
		$this->ref = $input;
	}
	function setDomain($input){
		$this->domain = $input;
	}
	function setActive($input){
		$this->active = $input;
	}
	function setCreatedOn($input){
		$this->created_on = $input;
	}
	function setUpdatedOn($input){
		$this->updated_on = $input;
	}
	function getId(){
		return $this->id;
	}
	function getName(){
		return $this->name;
	}
	function getRef(){
		return $this->ref;
	}
	function getDomain(){
		return $this->domain;
	}
	function getActive(){
		return $this->active;
	}
	function getCreatedOn(){
		return $this->created_on;
	}
	function getUpdatedOn(){
		return $this->updated_on;
	}
	
	function init($cmd, $input)
	{
		$ci=& get_instance();
		$this->current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('');
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_SITE)->result();
			$ci->db->set_dbprefix($this->current_dbprefix);
		}else{
			return false;
		}
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->name = $result[0]->name;
			$this->ref = $result[0]->ref;
			$this->domain = $result[0]->domain;
			$this->active = $result[0]->active;
			$this->created_on = $result[0]->created_on;
			$this->updated_on = $result[0]->updated_on;
			return $this;
		}
		return false;
	}
	
	function getSiteList(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('');
		$siteList =  $ci->db->get(TBL_SITE)->result();
		$ci->db->set_dbprefix($this->current_dbprefix);
		return $siteList;
	}
	
	function save(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('');
		$ci->db->set('name',$this->getName());
		$ci->db->set('ref',$this->getRef());
		$ci->db->set('domain',$this->getDomain());
		$ci->db->set('active',$this->getActive());
		$ci->db->set('created_on',$this->getCreatedOn());
		$ci->db->set('updated_on',$this->getUpdatedOn());
		$ci->db->insert(TBL_SITE);
		$id = $ci->db->insert_id();
		$ci->db->set_dbprefix($this->current_dbprefix);
		return $id;
	}
	function update(){
		$ci=& get_instance();
		$ci->db->set('name',$this->getName());
		$ci->db->set('ref',$this->getRef());
		$ci->db->set('domain',$this->getDomain());
		$ci->db->set('active',$this->getActive());
		$ci->db->set('created_on',$this->getCreatedOn());
		$ci->db->set('updated_on',$this->getUpdatedOn());
		return $ci->db->where('id', $this->id)->update(TBL_SITE);
	}
	function delete($id){
		$ci=& get_instance();
		$ci->db->where('id',$id);
		$ci->db->delete(TBL_SITE);
	}
	function count(){
		$ci = & get_instance();
		$this->current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('');
		$result = $ci->db->get(TBL_SITE)->num_rows();		
		$ci->db->set_dbprefix($this->current_dbprefix);
		return $result;
	}
}